﻿namespace UnitTestInjector
{
    #region Usings

    using Localizations;
    using System;

    #endregion Usings

    public abstract class BaseTestClass
    {
        protected virtual void TestExpectedArgumentException<TException>(Action testMethod,
            string parameterName)
            where TException : ArgumentException
        {
            if (testMethod == null)
            {
                throw new ArgumentNullException("testMethod",
                    Strings.TestExpectedArgumentExceptionParameterNameIsInvalidExceptionMessage);
            }

            if (string.IsNullOrWhiteSpace(parameterName))
            {
                throw new ArgumentException(Strings.TestExpectedArgumentExceptionParameterNameIsInvalidExceptionMessage,
                    "parameterName");
            }

            try
            {
                testMethod();
            }
            catch (TException ex)
            {
                UnitTestInjectorAssert.IsInstanceOfType<TException>(ex);
                if (ex is ArgumentNullException) UnitTestInjectorAssert.AreEqual(parameterName, ex.ParamName);
                else UnitTestInjectorAssert.AreEqual(parameterName, ex.Message);
                return;
            }

            throw new ArgumentException(Strings.TestExpectedArgumentExceptionNoExceptionRaised, "testMethod");
        }

        protected virtual void TestExpectedException<TException>(Action testMethod, string exceptionMessage)
            where TException : Exception
        {
            if (testMethod == null)
            {
                throw new ArgumentNullException("testMethod",
                    Strings.TestExpectedArgumentExceptionParameterNameIsInvalidExceptionMessage);
            }

            try
            {
                testMethod();
            }
            catch (TException ex)
            {
                UnitTestInjectorAssert.IsInstanceOfType<TException>(ex);
                UnitTestInjectorAssert.AreEqual(exceptionMessage, ex.Message);
                return;
            }

            throw new ArgumentException(Strings.TestExpectedArgumentExceptionNoExceptionRaised, "testMethod");
        }
    }
}